package ilincar.systemcore.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * Created by root on 16-5-20.
 */
public class SpUtil {
    private static final String NAME="Service_db";
    private static final String NAME_PUBLIC="Settings_public";
    private static SpUtil instance;
    static{
        instance=new SpUtil();
    }

    public static SpUtil getInstance(){
        if(instance==null){
            instance=new SpUtil();
        }
        return instance;
    }

    public static SharedPreferences getSharePerference(Context context){
        return context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
    }
    
    public static SharedPreferences getPublicSharePerference(Context context){
    	// Context.MODE_MULTI_PROCESS 实现跨进程间数据同步
        return context.getSharedPreferences(NAME_PUBLIC, Context.MODE_MULTI_PROCESS);
    }
    
    public static String getPublicStringSharePerference(Context context, String packagename, String filename, String key) {
    	String content = null;  
        Context c = null;  
        try {  
            c = context.createPackageContext(packagename,  
                    Context.CONTEXT_IGNORE_SECURITY);  
            SharedPreferences sp = c.getSharedPreferences(filename, Context.MODE_MULTI_PROCESS);
            Log.d("guoqz", "----------value--------" + sp.getString(key, ""));
            return sp.getString(key, "");
        } catch (NameNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
  
        return "";
    }
    
    public static float getPublicFloatSharePerference(Context context, String packagename, String filename, String key) {
    	String content = null;  
        Context c = null;  
        try {  
            c = context.createPackageContext(packagename,  
                    Context.CONTEXT_IGNORE_SECURITY);  
            SharedPreferences sp = c.getSharedPreferences(filename, Context.MODE_MULTI_PROCESS);
            Log.d("guoqz", "----------value--------" + sp.getFloat(key, 0));
            return sp.getFloat(key, 0);
        } catch (NameNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
  
        return 0;
    }
    
    public static void setPublicStringSharePerference(Context context, String packagename, String filename, String key, String value) {
    	String content = null;  
        Context c = null;  
        try {  
            c = context.createPackageContext(packagename,  
                    Context.CONTEXT_IGNORE_SECURITY);  
            SharedPreferences sp = c.getSharedPreferences(filename, Context.MODE_MULTI_PROCESS);
            SharedPreferences.Editor editor=sp.edit();
            editor.putString(key, value);
            editor.commit();
        } catch (NameNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }
    
    public static void setPublicFloatSharePerference(Context context, String packagename, String filename, String key, float value) {
    	String content = null;  
        Context c = null;  
        try {  
            c = context.createPackageContext(packagename,  
                    Context.CONTEXT_IGNORE_SECURITY);  
            SharedPreferences sp = c.getSharedPreferences(filename, Context.MODE_MULTI_PROCESS);
            SharedPreferences.Editor editor=sp.edit();
            editor.putFloat(key, value);
            editor.commit();
        } catch (NameNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }
    
    public static void setPublicBooleanSharePerference(Context context, String packagename, String filename, String key, boolean value) {
    	String content = null;  
        Context c = null;  
        try {  
            c = context.createPackageContext(packagename,  
                    Context.CONTEXT_IGNORE_SECURITY);  
            SharedPreferences sp = c.getSharedPreferences(filename, Context.MODE_MULTI_PROCESS);
            SharedPreferences.Editor editor=sp.edit();
            editor.putBoolean(key, value);
            editor.commit();
        } catch (NameNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }
    
    public static void setStringSharedPerference(Context context,String key,String value){
        SharedPreferences.Editor editor=getSharePerference(context).edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static String getStringSharedPerference(Context context,String key){
        return getSharePerference(context).getString(key, "");
    }
    
    public static String getPublicStringSharedPerference(Context context, String key){
        return context.getSharedPreferences(NAME_PUBLIC, Context.MODE_MULTI_PROCESS).getString(key, "");
    }
    
    public static void setFloatSharedPerference(Context context,String key,float value){
        SharedPreferences.Editor editor=getSharePerference(context).edit();
        editor.putFloat(key, value);
        editor.commit();
    }
    
    public static void setFloatSharedPerference(SharedPreferences sp,String key,float value){
        SharedPreferences.Editor editor=sp.edit();
        editor.putFloat(key, value);
        editor.commit();
    }
    
    public static float getFloatSharedPerference(Context context,String key){
        return getSharePerference(context).getFloat(key, 0);
    }
    
    public static float getFloatSharedPerference(SharedPreferences sp,String key){
        return sp.getFloat(key, 0);
    }

    public static void setIntSharedPerference(Context context,String key,int value){
        SharedPreferences.Editor editor=getSharePerference(context).edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static int getIntSharedPerference(Context context,String key){
        return getSharePerference(context).getInt(key, 0);
    }
    
    public static void setBooleanSharedPerference(Context context,String key,boolean value){
        SharedPreferences.Editor editor=getSharePerference(context).edit();
        editor.putBoolean(key, value);
        editor.commit();
    }
    
    public static boolean getBooleanSharedPerference(Context context ,String key){
        return getSharePerference(context).getBoolean(key, false);
    }

    public static void deleteAll(Context context){
        SharedPreferences.Editor editor=getSharePerference(context).edit();
        editor.clear();
        editor.commit();
    }
    /**发送推送ＩＤ时间*/
    public static final String KEY_PUSH_ID_TIME = "sendPushIdTime";

    public static long getLong(Context context, String key){
        return getSharePerference(context).getLong(key, 0);
    }

    public static void putLong(Context context, String key, long t) {
        SharedPreferences.Editor editor = getSharePerference(context).edit();
        editor.putLong(key, t);
        editor.commit();

    }
}
